package com.portal.incurridos.persistence.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.portal.incurridos.persistence.configuration.Environment;
import com.portal.incurridos.persistence.dao.EnvironmentDao;

/**
 * @author Alan Dávila
 * e-mail ustargab@gmail.com
 * date 28/11/2013 23:21:29
 */
@Component("environmentDaoImpl")
public class EnvironmentDaoHibernamteImpl implements EnvironmentDao {

	/**
	 * Managment bean of dao layer.
	 */
	@Resource(name = "sessionFactory")
    private SessionFactory factoria;

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.EnvironmentDao#get(java.lang.String)
	 */
	public final Environment get(final String enviroremt) {
		Environment environmentOut = null;
		Session session = null;
		try {
			session = factoria.getCurrentSession();
			environmentOut = (Environment) session.get(Environment.class, enviroremt);
			session.close();
		} catch (Exception e) {
			environmentOut = null;
		}
		return environmentOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.EnvironmentDao#getAll()
	 */
	@SuppressWarnings("unchecked")
	public final List<Environment> getAll() {
		List<Environment> environmentList = null;
		Session session = null;
		try {
			session = factoria.getCurrentSession();
			environmentList = session.createQuery("from Environment").list();
			session.close();
		} catch (Exception e) {
			environmentList = new ArrayList<Environment>();
		}
		return environmentList;
	}

}
